package com.chenjt.springai.ten_multiModal.controller;

import com.chenjt.springai.ten_multiModal.entity.vo.MessageVO;
import com.chenjt.springai.ten_multiModal.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2025/5/28 16:36
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {

    private final ChatHistoryRepository chatHistoryRepository;

    private final ChatMemory chatMemory;


    /**
     * 查询会话id记录列表
     * @param type
     * @return
     */
    @GetMapping("/{type}")
    public List<String> getCHatIds(@PathVariable("type") String type){
        return chatHistoryRepository.getChatIds(type);
    }


    /**
     * 查询指定会话id记录详情
     * @param type
     * @param chatId
     * @return
     */
    @GetMapping("/{type}/{chatId}")
    public List<MessageVO> getChatHistory(@PathVariable("type") String type, @PathVariable("chatId") String chatId){
        List<Message> messages = chatMemory.get(chatId);
        if(messages == null){
//            return List.of();
            return new ArrayList<>();
        }
        return messages.stream().map(MessageVO::new).collect(Collectors.toList());
    }

}
